package com.zpark.servlet_request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * 获取表单提交的数据
 */
@WebServlet("/form.do")
public class ServletRequestDemo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 获取表单提交的参数
         */
        /*
            request.getParameter(请求参数名字")
            注意：
                请求参数名字指的是input输入框的name属性值
                <input type="text" name="username" />
         */

        // 修改request缓冲区的字符集为UTF-8
        request.setCharacterEncoding("UTF-8");

        // 获取用户名
        String username = request.getParameter("username");
        System.out.println("username = " + username);
        // 获取用户密码
        String password = request.getParameter("password");
        System.out.println("password = " + password);
        // 获取用户性别
        String sex = request.getParameter("sex");
        System.out.println("sex = " + sex);

        // 获取多选框的值
        String[] languages = request.getParameterValues("language");
        System.out.println(Arrays.toString(languages));

        // 获取下拉选狂的值
        String city = request.getParameter("city");
        System.out.println(city);

    }
}
